package logic

import (
	"com-man-sys/models/cache"
	"context"
	"errors"
	"math/rand"
	"time"

	"com-man-sys/comrpc/comrpc"
	"com-man-sys/comrpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type SendSmsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewSendSmsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SendSmsLogic {
	return &SendSmsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *SendSmsLogic) SendSms(in *comrpc.SendSmsRequest) (*comrpc.SendSmsResponse, error) {
	// todo: add your logic here and delete this line

	code := rand.Intn(9000) + 1000
	//sms, err := pkg.SendSms(in.Mobile, strconv.Itoa(code))
	//if err != nil {
	//	return nil, err
	//}
	//if sms.Code != 2 {
	//	return nil, err
	//}

	cache.Rdb.Set(cache.Ctx, "sendSms"+in.Mobile, code, time.Hour*3)
	result, _ := cache.Rdb.Incr(cache.Ctx, "number"+in.Mobile).Result()
	if result > 1 {
		cache.Rdb.Expire(cache.Ctx, "sendSms"+in.Mobile, time.Hour*1)
		return nil, errors.New("验证码已发送，请稍后再试")
	}

	return &comrpc.SendSmsResponse{}, nil
}
